package com.logic.comment.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author cxy
 */
@Component
public class XsRedisUtils {
    private static final Logger log = LoggerFactory.getLogger(XsRedisUtils.class);
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    ////////////////////////////////////////////////////////////////////////////////////////////// comment
    public boolean hasKey(String key) {
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    ////////////////////////////////////////////////////////////////////////////////////////////// value
    public void set(String key, Object value) {
        try {
            String jsonStr = JSONObject.toJSONString(value, SerializerFeature.PrettyFormat);
            redisTemplate.opsForValue().set(key, jsonStr);
        } catch (Exception e) {
            log.error("redis set", e);
        }
    }

    public void set(String key, Object value, long timeout, TimeUnit unit) {
        try {
            String jsonStr = JSONObject.toJSONString(value, SerializerFeature.PrettyFormat);
            redisTemplate.opsForValue().set(key, jsonStr, timeout, unit);
        } catch (Exception e) {
            log.error("redis set", e);
        }
    }

    public <T> T getToBean(String key, Class<T> beanClass) {
        try {
            String jsonStr = redisTemplate.opsForValue().get(key);
            return JSONObject.parseObject(jsonStr, beanClass);
        } catch (Exception e) {
            log.error("redis getToBean", e);
            return null;
        }
    }

    public <T> List<T> getToList(String key, Class<T> beanClass) {
        try {
            String jsonStr = redisTemplate.opsForValue().get(key);
            return JSONObject.parseArray(jsonStr, beanClass);
        } catch (Exception e) {
            log.error("redis getToList", e);
            return null;
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////// list
    public <T> long lRightAdd(String key, T value) {
        try {
            String jsonStr = JSONObject.toJSONString(value, SerializerFeature.PrettyFormat);
            Long size = redisTemplate.opsForList().rightPush(key, jsonStr);
            return size == null ? 1 : size - 1;
        } catch (Exception e) {
            log.error("redis lRightAdd", e);
            return 0;
        }
    }

    public <T> long lRightAdd(String key, T value, long timeout, TimeUnit unit) {
        try {
            String jsonStr = JSONObject.toJSONString(value, SerializerFeature.PrettyFormat);
            Long size = redisTemplate.opsForList().rightPush(key, jsonStr);
            redisTemplate.expire(key, timeout, unit);
            return size == null ? 1 : size - 1;
        } catch (Exception e) {
            log.error("redis lRightAdd", e);
            return 0;
        }
    }

    public <T> T lIndexToBean(String key, Long index, Class<T> beanClass) {
        try {
            String jsonStr = redisTemplate.opsForList().index(key, index);
            return JSONObject.parseObject(jsonStr, beanClass);
        } catch (Exception e) {
            log.error("redis lIndexToBean", e);
            return null;
        }
    }

    public <T> List<T> lIndexToList(String key, Long index, Class<T> beanClass) {
        try {
            String jsonStr = redisTemplate.opsForList().index(key, index);
            return JSONObject.parseArray(jsonStr, beanClass);
        } catch (Exception e) {
            log.error("redis lIndexToList", e);
            return null;
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////// hash
    public <T> void hSet(String key, String hashKey, T value) {
        try {
            String jsonStr = JSONObject.toJSONString(value, SerializerFeature.PrettyFormat);
            redisTemplate.opsForHash().put(key, hashKey, jsonStr);
        } catch (Exception e) {
            log.error("redis hSet", e);
        }
    }

    public <T> void hSet(String key, String hashKey, T value, long timeout, TimeUnit unit) {
        try {
            String jsonStr = JSONObject.toJSONString(value, SerializerFeature.PrettyFormat);
            redisTemplate.opsForHash().put(key, hashKey, jsonStr);
            redisTemplate.expire(key, timeout, unit);
        } catch (Exception e) {
            log.error("redis hSet", e);
        }
    }

    public <T> T hGetBean(String key, String hashKey, Class<T> beanClass) {
        try {
            String jsonStr = (String) redisTemplate.opsForHash().get(key, hashKey);
            return JSONObject.parseObject(jsonStr, beanClass);
        } catch (Exception e) {
            log.error("redis hGetBean", e);
            return null;
        }
    }

    public <T> Map<String, T> hGetAllBean(String key, Class<T> beanClass) {
        try {
            Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
            if (entries.isEmpty()) {
                return null;
            }
            Map<String, T> map = new HashMap<>();
            entries.forEach((k, v) -> {
                String key2 = k.toString();
                String jsonStr = (String) v;
                T t = JSONObject.parseObject(jsonStr, beanClass);
                map.put(key2, t);
            });
            return map;
        } catch (Exception e) {
            log.error("redis hGetBean", e);
            return null;
        }
    }
    public Long hSize(String key) {
        return redisTemplate.opsForHash().size(key);
    }

    public <T> List<T> hGetList(String key, String hashKey, Class<T> beanClass) {
        try {
            String jsonStr = (String) redisTemplate.opsForHash().get(key, hashKey);
            return JSONObject.parseArray(jsonStr, beanClass);
        } catch (Exception e) {
            log.error("redis hGetList", e);
            return null;
        }
    }

    public void expire(String key1, int i, TimeUnit timeUnit) {
        redisTemplate.expire(key1, i, timeUnit);
    }

    public void delete(String format) {
        redisTemplate.delete(format);
    }
}
